//
//  DisCoverXuanShangSendViewController.m
//  随拍
//
//  Created by lanou3g on 16/1/18.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "DisCoverXuanShangSendViewController.h"
#import <CoreLocation/CoreLocation.h>


@interface DisCoverXuanShangSendViewController ()<CLLocationManagerDelegate,UITextViewDelegate,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property(nonatomic,strong) UILabel *placeLabel ;

@property(nonatomic,strong) UITextView *textView;

@property(nonatomic,strong)UIView *barView;

@property(nonatomic,strong)NSString *filePath;


@property(nonatomic,strong)CLLocationManager * locationManager;

@property(nonatomic,strong)UIButton * placeButton;

@property(nonatomic,strong)NSString *didian;

@property(nonatomic,assign)NSInteger timePlace ;


@property(nonatomic,strong)UIView *placeView;

@end

@implementation DisCoverXuanShangSendViewController

-(instancetype)init{
    
    if (self = [super init]) {
        
        self.hidesBottomBarWhenPushed=YES;
        
        
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
   // [self initializeLocationService];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"发送悬赏";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(leftButton:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(rightButton:)];
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height - 121)];
    
    _textView.delegate = self;
    
    //textfield.placeholder = @"邂逅旅途,分享感受";
    
    _textView.textAlignment = NSTextAlignmentLeft;
    
    self.placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 15, 200, 20)];
    
    _placeLabel.text = @"邂逅旅途,分享感受...";
    
    
    
    
    //_textField.backgroundColor = [UIColor redColor];
    
    self.barView = [[UIView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 105,self.view.frame.size.width,40)];
    
    _barView.layer.borderColor = [[UIColor grayColor] CGColor];
    
    _barView.layer.borderWidth = 0.7f;
    
    //_barView.backgroundColor = [UIColor cyanColor];
    
    UIButton *igButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    igButton.frame = CGRectMake(20, 5, 35, 30);
    
    [igButton addTarget:self action:@selector(igButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [igButton setTitle:@"相机" forState:UIControlStateNormal];
    
    //igButton.backgroundColor = [UIColor redColor];
    
    
    UIButton *careButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [careButton setTitle:@"相册" forState:UIControlStateNormal];
    
    careButton.frame = CGRectMake(60, 5, 35, 30);
    
    [careButton addTarget:self action:@selector(careButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.placeView = [[UIView alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height - 150, 130, 30)];
  
    
    
   // _placeView.backgroundColor = [UIColor cyanColor];
    
    self.placeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    _placeButton.frame = CGRectMake(30, 5, 280, 20);
    
    [_placeButton setTitle:@"显示位置" forState:UIControlStateNormal];
    
   // _placeButton.
    
    _placeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _placeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    [_placeButton addTarget:self action:@selector(placeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
   // _placeButton.backgroundColor = [UIColor blueColor];
    
    
    UIImageView *igView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 8, 20, 15)];
    
    igView.image = [UIImage imageNamed:@"cinema"];
    
    igView.contentMode = UIViewContentModeScaleAspectFit;
    
   // igView.backgroundColor = [UIColor orangeColor];
    
    [_textView addSubview:_placeLabel];
    [_placeView addSubview:_placeButton];
    [_placeView addSubview:igView];
    [_barView addSubview:igButton];
    [_barView addSubview:careButton];
    [self.view addSubview:_textView];
    [self.view addSubview:_barView];
    [self.view addSubview:_placeView];
                                                               
   
    
}



-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{

     //[textField ];
    
    NSTimeInterval animationDuration=0.30f;
    
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    
        [UIView setAnimationDuration:animationDuration];
    
        //float width = self.view.frame.size.width;
    
        //float height = self.view.frame.size.height;
    
        //上移n个单位，按实际情况设置
         CGRect rect=CGRectMake(0.0,self.view.frame.size.height - 110-210,self.view.frame.size.width,40);
    
    CGRect placerect = CGRectMake(0.0,self.view.frame.size.height - 150-210,self.view.frame.size.width,30);
    
          _barView.frame=rect;
    
    _placeView.frame = placerect;
    
         [UIView commitAnimations];
    
          [_placeLabel removeFromSuperview];
  
    
    
return YES;

}


-(BOOL)textViewShouldEndEditing:(UITextView *)textView{

    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    //float width = self.view.frame.size.width;
    
  //  float height = self.view.frame.size.height;
    //上移n个单位，按实际情况设置
    CGRect rect=CGRectMake(0.0,self.view.frame.size.height - 50,self.view.frame.size.width,40);

    
    CGRect placerect = CGRectMake(0.0,self.view.frame.size.height - 85,self.view.frame.size.width,30);
    
    _barView.frame=rect;
    
    _placeView.frame = placerect;
    
    [UIView commitAnimations];
    
    [_textView addSubview:_placeLabel];
    
    return YES;


    

}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}




//-(BOOL)textFieldShouldReturn:(UITextField *)textField{
//
//    
////    self.placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
////    
////    [_textfield addSubview:_placeLabel];
//    
//
//    [textField resignFirstResponder];
//    
//    
//    return YES;
//    
//}



-(void)placeButtonAction:(UIButton *)sender{

    if (_timePlace == 0) {
        
        NSLog(@"调用GPS");
        
        
        [self initializeLocationService];
        
        _timePlace++;
        
    }else{
    
        [_placeButton setTitle:@"显示位置" forState:UIControlStateNormal];
        
        _placeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _placeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        
        _timePlace = 0;
    
    }
    
   
    
   
    
}


- (void)initializeLocationService {
    
    // 初始化定位管理器
    _locationManager = [[CLLocationManager alloc] init];
    // 设置代理
    _locationManager.delegate = self;
    // 设置定位精确度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 设置过滤器为无
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 开始定位
    // 取得定位权限，有两个方法，取决于你的定位使用情况
    
    // 一个是requestAlwaysAuthorization，一个是requestWhenInUseAuthorization
    
    [_locationManager requestAlwaysAuthorization];//这句话ios8以上版本使用。
    
    [_locationManager startUpdatingLocation];
    
   
    
}




-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    NSString *dd = [NSString stringWithFormat:@"%lf",newLocation.coordinate.longitude];
    
    NSLog(@"dd = %@",dd);
    
    NSString * cc = [NSString stringWithFormat:@"%lf",newLocation.coordinate.latitude];
    
    NSLog(@"cc = %@",cc);
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
  
        if (placemarks.count > 0) {
            
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            
            self.didian = placemark.name;
            
            NSLog(@"didian == %@",_didian);
            
             [_placeButton setTitle:_didian forState:UIControlStateNormal];
            
            NSString *city = placemark.locality;
            
            if (!city) {
                
                city = placemark.administrativeArea;
                
            }
            
            NSLog(@"city = %@",city);
            
            NSString *city4 = city;
            
            NSLog(@"city4 == %@",city4);
            
            
        }else if (error == nil && [placemarks count] == 0){
        
            NSLog(@"00000");
        
        
        }else if (error != nil){
        
            NSLog(@"11111");
        
        }
    }];
    
    [manager stopUpdatingLocation];



}

















-(void)igButtonAction:(UIButton *)sender{

    NSLog(@"调用相机");
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        
        picker.delegate = self;
        
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self showViewController:picker sender:nil];
        
    }else{
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"抱歉" message:@"你的设备没有摄像头" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alert show];
        
        
        
    
    }
    
 
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ //调用相机拍摄完成后执行的方法

    NSString*type = [info objectForKey:UIImagePickerControllerMediaType];
    
    if ([type isEqualToString:@"public.image"]) {
        
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        
        NSData *data;
        
        if (UIImagePNGRepresentation(image) == nil) {
            data = UIImageJPEGRepresentation(image, 1.0);
            
        }else{
        
            data = UIImagePNGRepresentation(image);
        
        }
        
        NSString *DocumentsPath = [NSHomeDirectory()stringByAppendingString:@"Documents"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
        
        self.filePath = [[NSString alloc] initWithFormat:@"%@%@"
        ,DocumentsPath,@"/image.png"];
        
        
        NSLog(@"图片路径是 %@",_filePath);
        
        [picker dismissViewControllerAnimated:YES completion:^{
            
        }];
        
        UIImageView *smallimage = [[UIImageView alloc] initWithFrame:CGRectMake(120, 0, 40, 40)];
        
        smallimage.image = image;
        
        [_barView addSubview:smallimage];
        
      
        
    }
    
  
}





-(void)careButtonAction:(UIButton *)sender{

    NSLog(@"调用相册");
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        
        picker.delegate = self;
        
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        
        [self showViewController:picker sender:nil];
        
        
    }
    

}




-(void)leftButton:(UIBarButtonItem *)sender{

    [self.navigationController popViewControllerAnimated:YES];
    
}


-(void)rightButton:(UIBarButtonItem *)sender{
    
    
    

    NSLog(@"发送悬赏信息");
    
  
    
    
    
}



@end
